"""
自定义异常类
定义项目中使用的各种异常类型
"""
from typing import Any, Dict, Optional
from fastapi import HTTPException, status


class BaseCustomException(HTTPException):
    """基础自定义异常类"""
    
    def __init__(
        self,
        status_code: int,
        detail: Any = None,
        headers: Optional[Dict[str, Any]] = None,
    ) -> None:
        super().__init__(status_code=status_code, detail=detail, headers=headers)


class ValidationError(BaseCustomException):
    """数据验证异常"""
    
    def __init__(self, detail: str = "数据验证失败"):
        super().__init__(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=detail
        )


class NotFoundError(BaseCustomException):
    """资源未找到异常"""
    
    def __init__(self, detail: str = "资源未找到"):
        super().__init__(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=detail
        )


class PermissionDeniedError(BaseCustomException):
    """权限拒绝异常"""
    
    def __init__(self, detail: str = "权限不足"):
        super().__init__(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=detail
        )


class ConflictError(BaseCustomException):
    """资源冲突异常"""
    
    def __init__(self, detail: str = "资源冲突"):
        super().__init__(
            status_code=status.HTTP_409_CONFLICT,
            detail=detail
        )


class BusinessLogicError(BaseCustomException):
    """业务逻辑异常"""
    
    def __init__(self, detail: str = "业务逻辑错误"):
        super().__init__(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=detail
        )


# 版本管理相关异常
class VersionNotFoundError(NotFoundError):
    """版本未找到异常"""
    
    def __init__(self, version_id: str):
        super().__init__(detail=f"版本 {version_id} 未找到")


class VersionStatusError(BusinessLogicError):
    """版本状态异常"""
    
    def __init__(self, detail: str = "版本状态不允许此操作"):
        super().__init__(detail=detail)


class VersionReleaseError(BusinessLogicError):
    """版本发布异常"""
    
    def __init__(self, detail: str = "版本发布失败"):
        super().__init__(detail=detail)


class VersionRollbackError(BusinessLogicError):
    """版本回滚异常"""
    
    def __init__(self, detail: str = "版本回滚失败"):
        super().__init__(detail=detail)


class VersionRelationError(BusinessLogicError):
    """版本关联异常"""
    
    def __init__(self, detail: str = "版本关联操作失败"):
        super().__init__(detail=detail)


# 项目管理相关异常
class ProjectNotFoundError(NotFoundError):
    """项目未找到异常"""
    
    def __init__(self, project_id: str):
        super().__init__(detail=f"项目 {project_id} 未找到")


class TaskNotFoundError(NotFoundError):
    """任务未找到异常"""
    
    def __init__(self, task_id: str):
        super().__init__(detail=f"任务 {task_id} 未找到")


class DefectNotFoundError(NotFoundError):
    """缺陷未找到异常"""
    
    def __init__(self, defect_id: str):
        super().__init__(detail=f"缺陷 {defect_id} 未找到")


class UserNotFoundError(NotFoundError):
    """用户未找到异常"""
    
    def __init__(self, user_id: str):
        super().__init__(detail=f"用户 {user_id} 未找到")


# 数据库相关异常
class DatabaseError(BaseCustomException):
    """数据库操作异常"""
    
    def __init__(self, detail: str = "数据库操作失败"):
        super().__init__(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=detail
        )


class DuplicateKeyError(ConflictError):
    """重复键异常"""
    
    def __init__(self, detail: str = "数据已存在"):
        super().__init__(detail=detail)